export default {
    /**
     * 获取时间字符串。例如 2018-01-01 10:00:00
     * @param timestamp
     * @returns {string}
     */
    getTimeString(timestamp) {
        if (timestamp) {
            const time = new Date(timestamp)
            let dateStr = ''
            const year = time.getFullYear()
            const month = String(time.getMonth() + 1).length === 1 ? '0' + (time.getMonth() + 1) : (time.getMonth() + 1)
            const day = String(time.getDate()).length === 1 ? '0' + time.getDate() : time.getDate()
            dateStr = year + '-' + month + '-' + day
            let timeStr = ''
            const hours = time.getHours()
            const hh = String(hours).length === 1 ? '0' + hours : hours
            const minutes = time.getMinutes()
            const mm = String(minutes).length === 1 ? '0' + minutes : minutes
            const seconds = time.getSeconds()
            const ss = String(seconds).length === 1 ? '0' + seconds : seconds
            timeStr = `${hh}:${mm}:${ss}`
            return dateStr + ' ' + timeStr
        } else {
            return ''
        }
    },
    /**
     * 获取时间字符串，无分割。例如 20180101100000
     * @param timestamp
     * @returns {string}
     */
    getTimeString2(timestamp) {
        if (timestamp) {
            const time = new Date(timestamp)
            let dateStr = ''
            const year = String(time.getFullYear())
            const month = String(time.getMonth() + 1).length === 1 ? '0' + (time.getMonth() + 1) : (time.getMonth() + 1) + ''
            const day = String(time.getDate()).length === 1 ? '0' + time.getDate() : time.getDate() + ''
            dateStr = year + month + day
            let timeStr = ''
            const hours = time.getHours()
            const hh = String(hours).length === 1 ? '0' + hours : hours
            const minutes = time.getMinutes()
            const mm = String(minutes).length === 1 ? '0' + minutes : minutes
            const seconds = time.getSeconds()
            const ss = String(seconds).length === 1 ? '0' + seconds : seconds
            timeStr = `${hh}${mm}${ss}`
            return dateStr + timeStr
        } else {
            return ''
        }
    },
    /**
     * 获取身份证字符串，无分割
     * @param idCard
     * @returns {string}
     */
    validateIdCard(idCard) {
        //15位和18位身份证号码的正则表达式
        var regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
        return new Promise((resolve, reject) => {
            //如果通过该验证，说明身份证格式正确，但准确性还需计算
            if (regIdCard.test(idCard)) {
                if (idCard.length == 18) {
                    var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //将前17位加权因子保存在数组里
                    var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); //这是除以11后，可能产生的11位余数、验证码，也保存成数组
                    var idCardWiSum = 0; //用来保存前17位各自乖以加权因子后的总和
                    for (var i = 0; i < 17; i++) {
                        idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];
                    }
                    var idCardMod = idCardWiSum % 11;//计算出校验码所在数组的位置
                    var idCardLast = idCard.substring(17);//得到最后一位身份证号码
                    //如果等于2，则说明校验码是10，身份证号码最后一位应该是X
                    if (idCardMod == 2) {
                        if (idCardLast == "X" || idCardLast == "x") {
                            resolve(true)
                        } else {
                            resolve(false)
                        }
                    } else {
                        //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
                        if (idCardLast == idCardY[idCardMod]) {
                            resolve(true)
                        } else {
                            resolve(false)
                        }
                    }
                }
            } else {
                resolve(false)
            }
        })
    },
    /**
     * 金额千分符
     * @param money
     * @returns {string}
     */
    formatMoney(money) {
        if (money) {
            let moneyArr = (+money).toFixed(2).split(".");
            return moneyArr[0].replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') + "." + moneyArr[1]
        } else {
            return money
        }
    },
    /**
     * 手机号前端脱敏
     * @param phone
     * @returns {string}
     */
    formatPhoneNumber(phone) {
        if (phone) {
            return phone.slice(0, 3) + "****" + phone.slice(7)
        }
        
    },
    /**
     * 银行卡号四位分割
     * @param bankCard
     * @returns {string}
     */
    formatBankCard(bankCard) {
        if (bankCard && bankCard.length > 3) {
            let length = Math.floor(bankCard.length / 4);
            let resBankCard = "";
            for (let i = 0; i < length; i++) {
                resBankCard += bankCard.slice(i * 4, (i + 1) * 4) + " "
            }
            return resBankCard
        } else {
           return bankCard
        }
    },
    /**
     * 浮点数失精
     * 例如 formatFloat(5.8-0.9,2)
     * @param num
     * @returns {string}
     */
    formatFloat(f, digit) {
        let m = Math.pow(10, digit);
        return Math.round(f * m, 10) / m;
    }
    
}
